﻿using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Microsoft.AspNetCore.Authorization;

namespace Journeo_backend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [AllowAnonymous]
    public class ImageController : ControllerBase
    {
        private readonly RedisService _redisService;

        public ImageController(RedisService redisService)
        {
            _redisService = redisService;
        }

        [HttpPost("upload/base64")]
        [SwaggerOperation(Summary = "上传图片base64", Description = "上传图片到Redis")]
        [SwaggerResponse(200, "上传成功", typeof(Response<string>))]
        [SwaggerResponse(400, "上传失败，图片无效")]
        public async Task<IActionResult> UploadImage(string key, string base64Image)
        {
            var response = await _redisService.UploadImageAsync(key, base64Image);
            return Ok(response);
        }

        [HttpGet("download/{imageKey}")]
        [SwaggerOperation(Summary = "获取图片base64", Description = "使用imageKey从Redis下载图片")]
        [SwaggerResponse(200, "下载成功", typeof(Response<string>))]
        [SwaggerResponse(400, "下载失败，imageKey无效")]
        public async Task<IActionResult> DownloadImage(string imageKey)
        {
            var response = await _redisService.GetImageAsync(imageKey);
            return Ok(response);
        }

        [HttpDelete("delete/{imageKey}")]
        [SwaggerOperation(Summary = "删除图片", Description = "使用imageKey从Redis删除图片")]
        [SwaggerResponse(200, "删除成功", typeof(Response<string>))]
        [SwaggerResponse(400, "删除失败，imageKey无效")]
        public async Task<IActionResult> DeleteImage(string imageKey)
        {
            var response = await _redisService.DeleteImageAsync(imageKey);
            return Ok(response);
        }

        [HttpPost("upload/file")]
        [SwaggerOperation(Summary = "上传图片file", Description = "上传图片文件到Redis")]
        [SwaggerResponse(200, "上传成功", typeof(Response<string>))]
        [SwaggerResponse(400, "上传失败，图片无效")]
        public async Task<IActionResult> UploadImageFile(string key, IFormFile imageFile)
        {
            var response = await _redisService.UploadImageAsync(key, imageFile);
            return Ok(response);
        }
    }
}
